Identifying sources of memory retention

ABSTRACT

One embodiment relates to a method for identifying sources of memory retention in an executing application. A size of a set of objects is tracked over multiple periods. A period is determined to be a growth period if the size for the set of objects increases above a previous maximum size, and the number of growth periods is counted. The set of objects is flagged as having potential undesired object retention (a memory leak) if the number of growth periods is greater than a threshold number. Other embodiments are also disclosed.

BACKGROUND OF THE INVENTION

1. Field of the Invention

The present invention relates generally to computer systems.

2. Description of the Background Art

Undesired Retention of Limited Resources

One of the issues involved in information processing on computer systemsis the undesired retention of limited resources by computer programs,such as applications or operating systems. Typically, a computer systemis comprised of limited resources, regardless of whether the resourcesare physical, virtual, or abstract. Examples of such resources are CPUcycles, memory, disk space, file descriptors, socket port numbers,database connections or other entities that are manipulated by computerprograms.

A computer program may dynamically allocate resources for its exclusiveuse during its execution. When a resource is no longer needed, it may bereleased by the program. Releasing the resource can be done by anexplicit action performed by the program, or by an automatic resourcemanagement system.

Undesired Memory Retention and Memory Leaks

As mentioned above, one example of a managed resource is memory in acomputer system that may be allocated to programs at runtime. In otherwords, this portion of memory is dynamically managed. The entity thatdynamically manages memory is usually referred to as a memory manager,and the memory managed by the memory manager is often referred to as amemory “heap.” Portions of the memory heap may be allocated temporarilyto a specific program and then freed when no longer needed by theprogram. Freed portions are available for re-allocation.

In some programming languages and their associated runtimes, such as Cand C++ and others, the memory manager functionality is typicallyprovided by the application program itself. Any release of memory nolonger needed by the program is controlled by the programmer. Failure toexplicitly release unneeded memory results in memory being wasted, as itwill not be used by this or any other program. Program errors which leadto such wasted memory are often called “memory leaks.”

In other programming languages and their runtimes, such as Java, Eiffel,C sharp (C#) and others, automatic memory management is employed, ratherthan explicit memory release. Automatic memory management, popularlyknown in the art as “garbage collection,” is an active component of theruntime system associated with the implementation of these programminglanguages and their associated runtimes. Automatic memory managementsolves the problem of applications that do not explicitly releaseunneeded memory by automatically returning those unused portions ofmemory when there are no longer any references from a defined root setof data structures to the data structures allocated by the program inthat region of memory.

However, another problem can occur with automatic memory management—theretention of references to the data structures in portions of memory,data structures that will not be used in the future execution of theapplication. The references to these unused data structures in theseareas generally prevent the automatic garbage collector from re-claimingthe unused portions of memory. In the common vernacular, undesiredmemory retention in runtimes that support automatic memory management isalso referred to as “memory leaks.” For example, in the Java runtime,these are often referred to as “Java memory leaks.”

It is highly desirable to discover the presence of undesired memoryretention in an application. Moreover, after discovering the presence ofundesired memory retention in an application, it is highly desirable toquickly find the root cause of the problem and identify the root cause'ssurrounding context. One can then use this information to fix theproblem.

Despite the use of garbage collection, object retention problems, oftencalled “memory leaks,” frequently occur in Java applications. Thisundesired object retention may eventually cause a Java application tocrash when all memory resources are consumed. In the short term, theruntime's management of large numbers of unused objects causesapplication slow-down and higher costs for the application in productiondeployment. Similarly, applications based on other programming languagesand runtimes also have similar resource consumption problems.

It has been determined that undesired object retention (a memory leak)occurs when there are references to an object that make that objectreachable from a set of root objects in the application, but the objectwill never be used again by the application. In that case, theapplication could use the object if needed, but the application does notuse the object, so keeping the object in memory is a waste of memory.

The object that is able to be referenced from the root set of objects inthe application through either direct or indirect references isfrequently referred to as a “live” object. Other means or techniques ofdetermining liveness also exist, including reference counting, and thesealternate techniques may also be used.

For example, a Java application may create objects during a JavaEnterprise Edition, also referred to as J2EE (Java 2 Platform EnterpriseEdition), transaction and inadvertently cache references to thesecreated objects. If these objects are no longer used by the applicationafter the completion of the transaction, the references to the objectsremain in the cache, and the cache itself remains reachable from theroot set of objects, then these transaction-related objects continue tooccupy memory—undesired object retention. In this case, a little morefree (or unused) memory is lost (leaked) each time the applicationperforms such a J2EE transaction.

Moreover, such memory leaks frequently occur in deployed web andmission-critical applications that are actively serving customers. Theresultant crashes that occur when no additional memory is available forthe application directly impact customer service and sales.

Existing solutions to the above discussed problem are typically toointrusive to the application and/or do not give an operator enoughinformation about the root cause and context of the undesired objectretention (memory leak). Most of the existing solutions for locatingretained objects are applicable only in development environments becausei) their high overhead would be too intrusive to use with a deployedapplication in production, ii) their need for user interaction to helpin determining the undesired object retention's root cause and context,and/or iii) their lack of scalability.

Memory leak analysis of a running application should preferably beperformed with very low performance degradation, typically no more thanabout 5 percent usage of the critical program resources when theproduction system is highly utilized. In addition, the applicationshould not be made to pause for several seconds, and the analysis shouldnot require large amounts of memory.

In addition, operators of data centers, where web or othermission-critical applications are frequently hosted, are under pressureto diagnose problems quickly but the users themselves may not havesophisticated debugging knowledge. As such, it is desirable to be ableto indicate to a data center operator exactly where memory leaks may beoccurring, and to give such indication as early as possible. Root causeanalysis and identification of the context of the problem in theproduction environment helps to reduce the overall cost of fixing theproblem by enabling quick re-configuration or defect-repair of theapplication. Such detailed diagnosis avoids the requirement for costly,and often unsuccessful, attempts to recreate the problem in thedevelopment environment.

SUMMARY

One embodiment of the invention relates to a method for identifyingsources of undesired memory retention with low overhead in an executingapplication. A size for a set of objects is tracked over multipleperiods. A period is determined to be a growth period if the size forthe set of objects increases above a previous maximum size. The numberof growth periods is counted. The set of objects is flagged as having apotential memory leak if the number of growth periods is greater than afirst threshold number.

Other embodiments are also disclosed.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a schematic diagram of an exemplary computer system in thecontext of which an embodiment of the invention may be implemented.

FIG. 2A is a flow chart depicting an exemplary method for determiningsets of objects that may be contributing to undesired object retentionin an application in accordance with an embodiment of the invention.

FIG. 2B is a flow chart depicting an exemplary method of locatingsources of undesired memory retention in accordance with an embodimentof the invention.

FIG. 3 is a graph depicting an example of the size for a set of objectsover time or events in accordance with an embodiment of the invention.

DETAILED DESCRIPTION

The present disclosure provides an advantageous solution to theabove-discussed problems. In particular, methods and apparatus foridentifying undesirable memory retention and finding the root cause andcontext are disclosed. In accordance with an embodiment of theinvention, typical patterns known to appear with memory retaining(leaking) applications are detected, and sampling is used to minimizethe overhead required for the retention (leakage) detection.Advantageously, memory leak locations, root causes and contexts may bedetected with pinpoint specificity and with low overhead in accordancewith an embodiment of the invention.

Example Computer System

An embodiment of the invention may be implemented in the context of acomputer system, such as, for example, the computer system 60 depictedin FIG. 1. Other embodiments of the invention may be implemented in thecontext of different types of computer systems or other systems.

The computer system 60 may be configured with a processing unit 62, asystem memory 64, and a system bus 66 that couples various systemcomponents together, including the system memory 64 to the processingunit 62. The system bus 66 may be any of several types of bus structuresincluding a memory bus or memory controller, a peripheral bus, and alocal bus using any of a variety of bus architectures.

Processor 62 typically includes cache circuitry 61, which includes cachememories having cache lines, and pre-fetch circuitry 63. The processor62, the cache circuitry 61 and the pre-fetch circuitry 63 operate witheach other as known in the art. The system memory 64 includes read onlymemory (ROM) 68 and random access memory (RAM) 70. A basic input/outputsystem 72 (BIOS) is stored in ROM 68.

The computer system 60 may also be configured with one or more of thefollowing drives: a hard disk drive 74 for reading from and writing to ahard disk, a magnetic disk drive 76 for reading from or writing to aremovable magnetic disk 78, and an optical disk drive 80 for readingfrom or writing to a removable optical disk 82 such as a CD ROM or otheroptical media. The hard disk drive 74, magnetic disk drive 76, andoptical disk drive 80 may be connected to the system bus 66 by a harddisk drive interface 84, a magnetic disk drive interface 86, and anoptical drive interface 88, respectively. The drives and theirassociated computer-readable media provide nonvolatile storage ofcomputer readable instructions, data structures, program modules andother data for the computer system 60. Other forms of data storage mayalso be used.

A number of program modules may be stored on the hard disk, magneticdisk 78, optical disk 82, ROM 68, and/or RAM 70. These programs includean operating system 90, one or more application programs 92, otherprogram modules 94, and program data 96. A user may enter commands andinformation into the computer system 60 through input devices such as akeyboard 98 and a mouse 100 or other input devices. These and otherinput devices are often connected to the processing unit 62 through aserial port interface 102 that is coupled to the system bus 66, but maybe connected by other interfaces, such as a parallel port, game port, ora universal serial bus (USB). A monitor 104 or other type of displaydevice may also be connected to the system bus 66 via an interface, suchas a video adapter 106. In addition to the monitor, personal computerstypically include other peripheral output devices (not shown) such asspeakers and printers. The computer system 60 may also have a networkinterface or adapter 108, a modem 110, or other means for establishingcommunications over a network (e.g., LAN, Internet, etc.).

The operating system 90 may be configured with a memory manager 120. Thememory manager 120 may be configured to handle allocations,reallocations, and deallocations of RAM 70 for one or more applicationprograms 92, other program modules 94, or internal kernel operations.The memory manager may be tasked with dividing memory resources amongthese executables.

Exemplary Method of Identifying and Locating Suspected Memory Leaks

FIG. 2A is a flow chart depicting an exemplary method 200 of identifyingsources of undesired memory retention in accordance with an embodimentof the invention. The method 200 is used to analyze sets of objects inorder to identify whether each set is a potential “memory leak” (e.g., asource of undesired object retention). The determination of whether aset of objects is a potential memory leak is based on how the set ofobjects changes over time. More particularly, in accordance with anembodiment of the invention, such determination may be based on thepattern of growth and non-growth periods for the set of objects.

First, sets of objects may be defined (202) for the purpose of thismethod 200. There are various ways to define such sets. In anembodiment, a set of objects may be defined as objects aggregated into acollection class, such as a Java collection class. In anotherembodiment, a set of objects may be defined as all objects of aparticular type. In another embodiment, a set of objects may be definedto be all objects reachable from a given object in the application. Thepreceding definitions are not intended to be exhaustive. Other ways ofdefining sets of objects in accordance with embodiments of the inventionare also possible. Regardless of the definitions of the sets of objects,which sets are to be tracked and analyzed may be specified (204)automatically or by a user in accordance with an embodiment of theinvention.

The maximum set size over the time period is tracked (206) for each setof objects specified. For example, the size of a set of objects may beobtained by interfacing with the memory management system. The trackingmay be performed, for example, by sampling the size of the set ofobjects periodically over time. The frequency of the sampling may beconfigurable. Depending on situations, the sampling frequency may be setrelatively low so as to minimize the overhead imposed by the method 200(for example, to 5% overhead or less) while being sufficiently high toprovide a reasonably accurate tracking of the size of the set ofobjects.

Another way to track the size of a set of objects is to get the size ofthe set when the set of objects is modified. To reduce the overheadimposed by the method 200 of FIG. 2A, not every modification of the setneeds to result in obtaining the set size. For example, a counter may beused to count the times a new object was added to the set of objects. Asize check may be performed when the counter modulo CONSTANT1==0, whereCONSTANT1 is a constant number defining the frequency of size checks interms of new objects added. A lower value for CONSTANT1 results in morefrequent checking but higher overhead, while a higher value forCONSTANT1 results in less frequent checking and lower overhead.

The elapsed time may be recognized as a sequence of elapsed periods. Inaccordance with an embodiment, the length of each period may be a fixedlength of time. In accordance with another embodiment, the length ofeach period may be a variable length of time. The variable length oftime may depend on a configurable parameter. For example, the variablelength of time may depend on an application load. The application loadmay correspond to a level of activity or use of the application. Inaccordance with another embodiment, the “elapsed time” may be tracked interms of events (rather than in terms of actual time elapsed). Theevents may comprise running the automatic management software (“garbagecollection”) or may comprise other types of events.

When an end of the period is reached (208), all specified sets ofobjects in block 204 are analyzed as to whether they represent potentialsources of undesired memory (object) retention. For this purpose, whilethere are any unprocessed (not yet analyzed) sets of objects 210, thealgorithm picks or selects (212) one of such sets and performs (250) theanalysis of the selected set. After the analysis, the method goes backto block 210 to test for any unprocessed sets. After all sets of objectsare processed, the method updates 204 the sets of objects to beanalyzed. This step is done to ensure that any newly created sets ofobjects that satisfy the defined criteria in block 202 are incorporatedin the future analysis.

Regarding the analysis of a set of objects in block 250, a flow chartdepicting such an analysis is given in FIG. 2B in accordance with anembodiment of the invention. A determination (252) is made as to whetherthe period just completed (the “current” period) is considered to be a“growth period” for this set of objects. In accordance with anembodiment of the invention, a given period is considered to be a growthperiod if it is observed during this time period that the maximum sizefor the set of objects increased to a size above a previously observedmaximum size. If a period is not designated as a growth period, then itis designated as a “non-growth period.” An example showing periodsdesignated as growth and non-growth periods is discussed below inrelation to FIG. 3.

If the current period is determined to be a growth period, then acounter tracking the number of elapsed growth periods is incremented254. In addition, a peak period for this set of objects is set (256) tobe the current time period. The peak period is the time period with thehighest observed size for this set of objects. If the observed sizecorresponding to a growth period exceeds a previously observed maximum,then the new peak period corresponds to the most recently observedgrowth period.

When the time period is not a growth period, and also after adjustingthe peak period in block 256 a check (258) is performed as to whetherthis set of objects has been previously flagged (264). In the case theset has not been flagged, a determination (260) is then made as towhether the number of elapsed growth periods is greater than a firstthreshold number N1. The threshold number N1 may be a configurableparameter. In an embodiment, N1 may be different depending upon the setof objects being tracked. In another embodiment, N1 may be the same foreach set of objects being tracked.

Lowering N1 for a set of objects makes the method 250 more sensitive toa possible growth trend in that set of objects, so that a potentialmemory leak in that set of objects would be flagged earlier. Raising N1for a set of objects makes the method 250 less sensitive to a possiblegrowth trend in that set of objects, so that false positive indicationsof potential memory leaks in that set of objects become less likely. Thevalue of constant N1 may also be dynamically calculated by the systemover time as the application runs or be reset by a mechanism that allowsadjustment of the value from outside of the process, for any of a set ofobjects or for all objects.

If the number of elapsed growth periods is still less than or equal toN1, then no memory leak is indicated (262) at this time in this set ofobjects. This is because the number of growth periods observed is so farinsufficient to warrant concern of a memory leak therein.

On the other hand, if the number of elapsed growth periods is determined(260) to be greater than N1, then the set of objects is flagged (264),and indicated (266) as a potential memory leak. The flag indicates thata sufficient number of growth periods have been observed to warrantconcern of a memory leak in this set of objects. The memory leakindication (266) may be sent to a system operator or other user. Thememory leak indication (266) may also be used by the running system toreconfigure or correct the problem.

To further specify the source of undesired memory retention to the user,a more specific analysis may be performed. In particular, contextinformation related to the set of objects may be made available to asystem operator or other user. This context information may comprise theage of the set of objects, measured in time periods, the current size ofthe set of objects, and/or stack trace or other information identifyingthe location in the program source code where the set of objects wasrecently enlarged. This above list is presented for illustrationalpurpose only, and it is not exhaustive. To reduce or limit the overheaddesired to locate the root cause of the undesired object retention, thecontext information may be obtained after a set of objects is flagged inblock 264, or, in other words, for the first time identified in block266 as having unconstrained growth. Of course, this in no way limits thefrequency with which the context information can be collected. Contextinformation can be collected and stored at regular or irregularintervals subsequent to the identification of the object as havingunconstrained growth. Per the embodiment depicted in FIG. 2B, a set ofobjects is indicated as having unconstrained growth when block 266 isreached.

In an implementation, the context information may be obtained bycapturing a series of calls from one part of a program to another partof the program. A determination may then be made as to whether the callsresult in adding objects to the previously identified set of objects.Those calls adding objects to that set may be a root cause of theundesired object retention.

Returning the discussion back to block 258, if this set of objects hasbeen flagged, then a determination may be made as to whether sufficienttime has elapsed since the most recently observed growth period todetermine whether the set of objects discontinued the memory retentiontrend. More particularly, a number of periods since the most recentgrowth period may be determined by subtracting the period number of thepeak period from the period number of the current period. The number ofperiods since the peak period is then compared (268) against a secondthreshold number N2.

The threshold number N2 may be a configurable parameter. In anembodiment, N2 may be different depending upon the set of objects beingtracked. In another embodiment, N2 may be the same for each set ofobjects being tracked. Increasing the value of N2 for a set of objectsmakes the method 250 “remember for a longer time” the recent growthperiods, so that the method 250 becomes more sensitive to (i.e. can lesseasily ignore) infrequent or smaller leaks. Decreasing the value of N2for a set of objects makes the method 250 “remember for a shorter periodof time” the recent growth periods, so that the method 250 tends todetect the more frequent or larger leaks, while tending to filter outthe infrequent or smaller leaks.

If the number of periods since the peak period is greater than thesecond threshold number N2, then no memory leak is indicated (262) atthis time in this set of objects. This is because a sufficient number ofperiods have elapsed since the most recently observed growth period. Inother words, no growth period has been observed for a sufficiently longtime.

On the other hand, if the number of periods since the peak period isless than or equal to the second threshold number N2, then a potentialmemory leak is indicated (266) in this set of objects. The indicationmay be sent to a system operator or other user.

FIG. 3 is a graph depicting an example of the size of a set of objectsover time or events in accordance with an embodiment of the invention.The number of objects in the set 302 as a function of time/events isgraphed (solid line). The time/event periods are shown by the verticaldashed lines. In this example, the length of each period is the same.Non-growth periods 308 and growth periods 310 are shown. A growth period310 is a period where the size 302 of the set of objects reaches a newhigh. In other words, a growth period 310 is a period where the numberof objects 302 is observed to exceed a previous maximum observed size.

Example Implementation

An example implementation of the methods 200 and 250 in FIGS. 2A and 2Bis now described in accordance with an embodiment of the invention. Inthis example, the sets of objects to be tracked for memory leak purposesare determined by the objects belonging to standard Java collectionclasses. For each such object, the set of objects to be analyzed bymethod 200 is defined as the objects referenced (or “contained”) by thecollection object. Therefore, the set of objects to be analyzed may beconsidered identical with the objects belonging to the Java collectionclasses. For each collection class, a “wrapper” class may be used whichis a subclass of the original class. When the application classes areloaded for execution, or dynamically as the application is executing,each creation of the collection object in the Java bytecodes is replacedwith the creation of the corresponding wrapper (or suitably modified)collection object.

For example, if the original application creates an object of a“java.util.Hashtable” class, then the instrumented application (i.e. theapplication as modified in accordance with an embodiment of theinvention) will create an object of a “wrappers.java.util.Hashtable”class, where “wrappers.java.util.Hashtable” is a subclass of“java.util.Hashtable.” Similarly, if the original application declares asubclass of “java.util.Hashtable,” the instrumented application willdeclare a subclass of “wrappers.java.util.Hashtable” instead.

The wrapper class includes additional fields (in addition to theinherited fields of the original class). For example, the additionalfields may include:

numberOfGrowthPeriods;

maximumSize;

peakPeriod;

counter; and

flaggedPotentialObjRetention.

The numberOfGrowthPeriods field may be used to count the number ofgrowth periods observed. The maximumSize field may be used to track themaximum size ever observed. The peakPeriod field may be used foridentification of the period in which the maximum size was observed.

The counter field may be used to count the times a new object was addedto the set of objects. Such a counter may be used to determine when toperform a check for a memory leak in the set of objects. For example, acheck for a memory leak in the set of objects may be performed when thecounter modulo CONSTANT1==0, where CONSTANT1 is a constant number. Alower value for CONSTANT1 results in more frequent checking but higheroverhead, while a higher value for CONSTANT1 results in less frequentchecking and lower overhead. The value of CONSTANT1 may also bedynamically calculated by the system over time as the application runsor be reset by a mechanism that allows adjustment of the value fromoutside of the process, for any of a set of objects or for all objects.

The flaggedPotentialObjRetention field may be used to indicate that theset of objects has been identified as a potential memory leak in thepast. See the use of the flag as discussed above in relation to FIG. 2B.

The following provides example pseudo-code indicating how theleak-locating instrumentation may be implemented in accordance with anembodiment of the invention.

package wrappers.java.util; public class Hashtable extendsjava.util.Hashtable { private transient int numberOfGrowthPeriods;private transient int maximumSize; private transient int peakPeriod;private transient int counter; private transient booleanflaggedPotentialObjRetention; public Object put (Object key, Objectvalue) { Object old = super.put(key,value); if (old == null) { // A newobject has been added counter++; if (counter % CONSTANT1 == 0)check_for_memory_leak( ); } return old; } private synchronized voidcheck_for_memory_leak( ) { if (currentTimePeriod( ) != peakPeriod) { intsize = super.size( ); if (size > maximumSize) { maximumSize = size;peakPeriod = currentTimePeriod( ); numberOfGrowthPeriods++; if(numberOfGrowthPeriods > N1 && !flaggedPotentialObjRetention) {flaggedPotentialObjRetention = true;CentralRepository.reportObject(this); } } } } public long getPeakPeriod() { return peakPeriod; } } package wrappers.java.util; importjava.util.ArrayList; import java.io.*; importjava.lang.ref.WeakReference; import wrappers.java.util.*; public classCentralRepository { static class LeakEntry { int objId; // The id of theflagged collection object WeakReference wref; // Reference to flaggedcollection object String strace; // The stack trace of the leak locationLeakEntry(int id, Object obj, String s) { objId = id; wref = newWeakReference(obj); strace = s; } } private static ArrayList leakList =new ArrayList( ); private static int objId = 0; private static intleakId = 0; public static void reportObject(Object obj) { // Try toobtain the stack trace where the recent // object reference was added tothe collection. String strace = null; try { throw new Exception( ); }catch (Exception exc) { // Obtain the exception throw stack traceStringWriter strWriter = new StringWriter( ); exc.printStackTrace( newPrintWriter(strWriter) ); strace = strWriter.toString( ); // The thirdframe should be the addition method // in the wrapper class. There couldbe more than // one frame that has this same method name/signature //(this method gets overridden in the subclasses). // So we need to parsethe stacktrace to find // the first frame whose method is different fromthe // wrapper's add/put method. This is the leak location. strace =getLeakLocation(strace); } synchronized (leakList) { leakList.add( newLeakEntry(leakId++, obj, strace) ); } } // Called periodically to checkwhich objects in the leakList // no longer satisfy the leaking conditionand should be reported // as not leaking (REPORT_NO_LEAK). // publicstatic void checkLeaks( ) { // Remove null entries in the leakListsynchronized ( leakList ) { Iterator ite = leakList.iterator( ); while (ite.hasNext( ) ) { LeakEntry entry = (LeakEntry) ite.next( ); ObjectcolObj = entry.wref.get( ); if ( colObj == null ) { // Report that thepreviously reported leak is gone REPORT_NO_LEAK(entry.objId);ite.remove( ); } } } // Go through the leakList to report those objectsthat // satisfy the leak condition as potential leaks (REPORT_LEAK) //and report those objects that do not satisfy the leak // condition(REPORT_NO_LEAK). Object[] entries = leakList.toArray( ); for ( int i =0; i < entries.length; i++ ) { LeakEntry entry = (LeakEntry) entries[i];Hashtable colObj = (Hashtable) entry.wref.get( ); if ( colObj != null ){ long peakPeriod = colObj.getPeakPeriod( ); if (currentTimePeriod( ) −peakPeriod <= N2 ) { // Report this collection object as a leak int size= colObj.size( ); REPORT_LEAK(colObj, entry.objId, size, entry.strace);} else { // Report that this object is no longer a leakREPORT_NO_LEAK(entry.objId); } } } } }

The above example code utilizes “wrappers.java.util.Hashtable” as asubclass of “java.util.Hashtable” and uses the additional fieldsdiscussed above. In the example pseudo-code, upon flagging of apotential memory leak in java.util.Hashtable, a report of the potentialmemory leak is sent to a central repository. Once reported to thecentral repository, a periodic check for memory leaks may be performedusing the formula (currentTimePeriod( ) minus object.peakPeriod is lessthan or equal to the second threshold number N2). (As discussed above inrelation to block 268 in FIG. 2B.)

In the CentralRepository class, a java.util.ArrayList object leakList,is used to track the flagged collection objects (potential leaks). Eachflagged object is represented by a LeakEntry, which includes the id usedto identify the collection object, the leak location stack trace and aWeakReference to the collection object. A WeakReference is used so thatthe reference held will become null when the collection object getsgarbage collected. When a collection object is flagged to be a potentialleak, the reportObject method of the CentralRepository class is calledto add this object to the leakList. In this method, the leak locationstack trace is obtained by throwing and catching an exception. Theaddition method in the collection class is used to add references to thecollection object. When the addition method is frequently called in theapplication, the collection size keeps growing, and will eventuallycause the collection object to be flagged. This call site is treated asthe leak source, the root cause of the undesired object retention.

Periodically, the method checkLeaks( ) is called to check whether: 1.the objects in the leakList satisfy the leaking condition and need to bereported; and 2. the objects that have been garbage collected need toremoved from the leakList. The leaking condition was stated before,currentTimePeriod minus object.peakPeriod is less than or equal to thethreshold N2. If the collection object satisfies this condition, it isreported as a leak with the leaking context (the leak location stacktrace, the collection size, etc.). If the object does not satisfy thecondition then it is reported as an object that is not a leak. If theobject was garbage collected, it is removed from the leakList.

The above-disclosed solution has various advantages. It may beimplemented with very low overhead, making it practical for continuousmonitoring of deployed applications. In addition, it may be used toprovide a user with the information to help understand where the leak islocated and also how and why the memory leak is occurring. The alertingand analysis may be performed automatically by the instrumentation, withno user interaction.

In the above description, numerous specific details are given to providea thorough understanding of embodiments of the invention. However, theabove description of illustrated embodiments is not intended to beexhaustive or to limit the invention to the precise forms disclosed. Oneskilled in the relevant art will recognize that the invention can bepracticed without one or more of the specific details, or with othermethods, components, etc. In other instances, well-known structures oroperations are not shown or described in detail to avoid obscuringaspects of the invention. While specific embodiments of, and examplesfor, the invention are described herein for illustrative purposes,various equivalent modifications are possible within the scope of theinvention, as those skilled in the relevant art will recognize.

These modifications can be made to the invention in light of the abovedetailed description. The terms used in the following claims should notbe construed to limit the invention to the specific embodimentsdisclosed in the specification and the claims. Rather, the scope of theinvention is to be determined by the following claims, which are to beconstrued in accordance with established doctrines of claiminterpretation.

1. A method for identifying sources of memory retention in an executingapplication, the method comprising: tracking a size for a set of objectsover multiple time periods; determining a period to be a growth periodif the size for the set of objects increases above a previous maximumsize, otherwise determining a period to be a non-growth period; countinga number of growth periods within the multiple time periods; flaggingthe set of objects if the number of growth periods is greater than afirst threshold number of growth periods; indicating a potentialundesired object retention for the set of objects if the set of objectsis flagged and the number of elapsed non-growth periods from the mostrecent growth period is less than or equal to a second threshold number,the second threshold number being a number of non-growth periods; andlocating a source of the memory retention by finding a cause of aresource consumption growth.
 2. The method of claim 1, furthercomprising limiting overhead to find the root cause by restricting afrequency at which said finding operation is performed for eachidentified set of objects.
 3. The method of claim 2, further comprisingdetermining a context of the root cause after the set of objects isflagged as having unconstrained growth.
 4. The method of claim 1,further comprising determining a number of elapsed time periods from amost recent growth period.
 5. The method of claim 1, wherein trackingthe size of the set of objects is performed using sampling.
 6. Themethod of claim 5, further comprising incrementing a counter when a newobject is added to the set of objects by the application.
 7. The methodof claim 6, further comprising determining what the time to track thesize of the set of objects is when the counter reaches a multiple of aconstant number.
 8. The method of claim 1, wherein the set of objectscomprises objects aggregated by a collection class.
 9. The method ofclaim 8, further comprising declaration of a wrapper class which is asubclass of the collection class.
 10. The method of claim 9, wherein thewrapper class comprises additional fields including at least the numberof growth periods, a maximum size observed, and a flag for indicatingthat the first threshold has been exceeded.
 11. The method of claim 10,wherein the additional fields further include a peak period identifierfor determining if a number of elapsed periods from a peak period isless than or equal to a second threshold number.
 12. The method of claim1, wherein the set of objects whose size is to be tracked comprises allobjects of a particular type.
 13. The method of claim 1, wherein the setof objects comprises all objects reachable from a given object in theapplication.
 14. The method of claim 1, wherein the set of objects isspecified by a user.
 15. The method of claim 1, wherein a length of eachperiod is a fixed time period.
 16. The method of claim 1, wherein alength of each period is a variable time period in dependence on aconfigurable parameter.
 17. The method of claim 16, wherein theconfigurable parameter comprises an application load.
 18. An apparatusconfigured with a processor for identifying sources of memory retention,the apparatus comprising: processor-executable code configured to tracka size for a set of objects over multiple periods; processor-executablecode configured to determine a period to be a growth period if the sizefor the set of objects increases above a previous maximum size, andotherwise indicating a period to be a non-growth period;processor-executable code configured to count a number of growth periodsover the multiple periods; processor-executable code configured to flagthe set of objects if the number of growth periods is greater than afirst threshold number of growth periods; processor-executable codeconfigured to determine a number of elapsed non-growth periods from amost recent growth period; and processor-executable code configured toindicate a potential memory leak in the set of objects if the set ofobjects is flagged and the number of elapsed non-growth periods from themost recent growth period is less than or equal to a second thresholdnumber, the second threshold number being a number of non-growthperiods; processor-executable code to locate a source of the memoryretention by finding a cause of a resource consumption growth.
 19. Asystem configured with a processor for locating sources of memoryretention, comprising: means for observing a size for a set of objectsover multiple periods; means for designating a period as a growth periodif the size of the set of objects increases above a previous maximumsize, and otherwise designating a period as a non-growth period; meansfor tracking a number of growth periods within the multiple timeperiods; means for setting a flag corresponding to the set of objects ifthe number of growth periods is greater than a first threshold number ofgrowth periods; means for calculating a number of elapsed non-growthperiods from a most recent growth period; and means for reporting apotential memory leak in the set of objects if the set of objects isflagged and the number of elapsed non-growth periods from the mostrecent growth period is less than or equal to a second threshold number,the second threshold number being a number of non-growth periods; andmeans for locating a source of the memory retention by finding a causeof a resource consumption growth.